package com.pic.struts2.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.base.Pagination.Pagination;
import com.fieldIndustry.pojo.Downloadcate;
import com.fieldIndustry.service.IDownloadcateService;
import com.opensymphony.xwork2.ActionSupport;
import com.pic.pojo.Pic;
import com.pic.service.IPicService;

public class PicAction  extends ActionSupport{
	
	/**
	 * @author L412 cy
	 * @since：20120830
	 * 图库图元新增、更新、上传；客户端访问列表、下载
	 */
	private static final long serialVersionUID = 1L;
	private  Pic pic;
	private IPicService<Pic> picSer;	
	public List<Pic> picList = new ArrayList<Pic>();
	private Downloadcate category;
	private IDownloadcateService<Downloadcate> downloadcateSer;
	private Pagination pagination; //分页工具条
	private boolean flag = false;
	private String result;
	
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private String imagePath;
	
	//客户端访问列表
	public String list()
    {
    	picList = picSer.list("from Pic",0,Integer.MAX_VALUE,null);
    	
    	return "list";
    }
	public String listAd()
    {
    	picList = picSer.list("From Pic", 0, Integer.MAX_VALUE, null);
    	
    	return "admin";
    }

	public String save()
	{
		category = (Downloadcate) downloadcateSer.find(Downloadcate.class, 2);
		pic.setCategory(category);

		copy(this.getUpload(), getImageFile());
		pic.setPath(imagePath+ this.getUploadFileName());
		flag = picSer.create(pic);
		if(flag){
			
		}else{
			
		}
		return listAd();
	}
	
    


	public File getImageFile() {
	
		File dir = new File(imagePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File imageFile = new File(dir, new String(uploadFileName));
		imageFile.deleteOnExit();
		try {
			imageFile.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return imageFile;
	}

	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src));
				out = new BufferedOutputStream(new FileOutputStream(dst));
				byte[] buffer = new byte[70 * 1024* 1024];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Pic getPic() {
		return pic;
	}
	public void setPic(Pic pic) {
		this.pic = pic;
	}
	public IPicService<Pic> getPicSer() {
		return picSer;
	}
	public void setPicSer(IPicService<Pic> picSer) {
		this.picSer = picSer;
	}
	public List<Pic> getPicList() {
		return picList;
	}
	public void setPicList(List<Pic> picList) {
		this.picList = picList;
	}
	public Downloadcate getCategory() {
		return category;
	}
	public void setCategory(Downloadcate category) {
		this.category = category;
	}
	public IDownloadcateService<Downloadcate> getDownloadcateSer() {
		return downloadcateSer;
	}
	public void setDownloadcateSer(
			IDownloadcateService<Downloadcate> downloadcateSer) {
		this.downloadcateSer = downloadcateSer;
	}
	public Pagination getPagination() {
		return pagination;
	}
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getImagePath() {
		return imagePath;
	}
	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
	

}
